package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;


@Slf4j
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult findList(WmSensitiveDto dto) {
        dto.checkParam();
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(dto.getName()), WmSensitive::getSensitives, dto.getName());
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        page = page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),  (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            log.info("新增的敏感词为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //查询敏感词是否存在
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<WmSensitive>().eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive sensitive = wmSensitiveMapper.selectOne(queryWrapper);
        if (sensitive !=null){
            log.info("敏感词已存在,不能保存");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //敏感词不存在，则保存
        wmSensitive.setCreatedTime(new Date());
        wmSensitiveMapper.insert(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateSeneitive(WmSensitive wmSensitive) {
        if (wmSensitive == null){
            log.info("传入改更新敏感词不为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @Transactional
    public ResponseResult delete(Integer id) {
        log.info("根据敏感词id{}删除",id);
        WmSensitive sensitive = wmSensitiveMapper.selectById(id);
        if (sensitive == null){
            log.info("敏感词id为{}的数据不存在");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        wmSensitiveMapper.deleteById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
